package com.csproj.system.repository.impl;


import com.csproj.system.domain.UserInfo;
import com.csproj.system.repository.custom.UserInfoRepositoryCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/31.
 */
public class UserInfoRepositoryImpl implements UserInfoRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }

    /**
     * 用户信息 分页查询方法
     * @param queryParams   参数
     * @param pageable      分页对象
     * @return
     */
    public Page<UserInfo> findPageList(Map<String, Object> queryParams, Pageable pageable) {
        //拼hql
        String hsql = " from UserInfo ui left join fetch ui.roleInfos as ri where ui.deletedFlag = 0 ";
        Map<String, Object> hsqlMap = new HashMap<>();

        String keyWord = (String) queryParams.get("keyWord");
        String roleId = (String) queryParams.get("roleId");
        String siteId = (String) queryParams.get("siteId");

        if (StringUtils.isNotBlank(keyWord)) {
            hsql += "and (ui.account like :account) ";
            hsqlMap.put("account", "%"+keyWord+"%");
        }

        if(StringUtils.isNotBlank(siteId)){
            hsql += " and ui.siteId = :siteId";
            hsqlMap.put("siteId",siteId);
        }

        if(StringUtils.isNotBlank(roleId)){
            hsql += " and ri.id = :roleId";
            hsqlMap.put("roleId",roleId);
        }
        //查询
        Page<UserInfo> feildList = query(hsql,pageable,hsqlMap);
        //返回
        return feildList;
    }
}
